import compose from './compose';
/**
 * 应用中间件来创建仓库
 * @param {*} middleware  中间件logger 
 * @returns 
 */
function applyMiddleware(...middlewares){
    return function (createStore){
        return function(reducer){
            //先创建store
            let store = createStore(reducer);
            let dispatch;
            let storeAPI = {
                getState:store.getState,
                dispatch:(action)=>dispatch(action)
            }
            const chain = middlewares.map(middleware=>middleware(storeAPI))
            //根据中间件创建新的dispatch
            dispatch = compose(...chain)(store.dispatch)
            return {
                ...store,
                dispatch
            };
        }
    }
}
export default applyMiddleware;